package com.tiancheng.trade.order.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @Author: likailun
 * @Description: 敏感数据处理
 * @Date: create in 2022/1/18 3:38 下午
 */

public class SensitiveDataUtil {
    /**
     * 手机号码前三后四脱敏
     * @param mobile
     * @return
     */
    public static String mobileEncrypt(String mobile) {
        if(mobile==null || mobile.isEmpty()){
            return "";
        }
        char[] chars = mobile.toCharArray();
        for(int i=0 ;i<chars.length;i++){
            if(i>2 && i< chars.length-3){
                chars[i]='*';
            }
        }
        return String.valueOf(chars);
    }

    //身份证前三后四脱敏
    public static String idEncrypt(String id) {
        if (StringUtils.isEmpty(id) || (id.length() < 8)) {
            return id;
        }
        return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
    }

    /**
     * 用户信息脱敏
     * @param name
     * @return
     */
    public static String userNameEncrypt(String name) {
        if(name==null || name.isEmpty()){
            return "";
        }
        char[] chars = name.toCharArray();
        if(chars.length==1){
                return name;
        }
        if(chars.length==2){
                return name.replaceFirst(name.substring(1), "*");
        }
        return name.replaceAll(name.substring(1, chars.length-1), "*");
    }

}
